package com.example.fengyungo.service.impl;

import com.example.fengyungo.constant.OrderConstant;
import com.example.fengyungo.dao.OrderDao;
import com.example.fengyungo.dao.ThingDao;
import com.example.fengyungo.domain.entity.Order;
import com.example.fengyungo.domain.pojo.Thing;
import com.example.fengyungo.domain.vo.OrderDetailsVO;
import com.example.fengyungo.result.Result;
import com.example.fengyungo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private ThingDao thingDao;

    /**
     * 获取所有订单
     *
     * @return 订单列表
     */
    @Override
    public Result<List<OrderDetailsVO>> getAccessOrder() {
        List<Order> orders = orderDao.getAccessOrder();
        List<OrderDetailsVO> orderDetailsVOS = new ArrayList<>();
        for (Order order : orders) {
            Thing thing = thingDao.getThingByNumber(order.getNumber());
            if (thing == null) {
                return Result.error(OrderConstant.ORDER_INFO_ERROR);
            }
            OrderDetailsVO orderDetailsVO = OrderDetailsVO.builder()
                    .userId(order.getUserId())
                    .receiverId(order.getReceiverId())
                    .number(order.getNumber())
                    .begin(order.getBegin())
                    .end(order.getEnd())
                    .earliestTime(order.getEarliestTime())
                    .latestTime(order.getLatestTime())
                    .note(order.getNote())
                    .price(order.getPrice())
                    .payStatus(order.getPayStatus())
                    .orderStatus(order.getOrderStatus())
                    .credit(order.getCredit())
                    .image(thing.getImage())
                    .length(thing.getLength())
                    .width(thing.getWidth())
                    .height(thing.getHeight())
                    .weight(thing.getWeight())
                    .build();
            orderDetailsVOS.add(orderDetailsVO);
        }
        return Result.success(orderDetailsVOS);
    }

    /**
     * 获取司机的订单
     *
     * @param receiverId 司机id
     * @return 订单列表
     */
    @Override
    public Result<List<OrderDetailsVO>> getOrdersByDriver(String receiverId) {
        List<Order> orders = orderDao.getOrdersByDriver(receiverId);
        List<OrderDetailsVO> orderDetailsVOS = new ArrayList<>();
        for (Order order : orders) {
            Thing thing = thingDao.getThingByNumber(order.getNumber());
            if (thing == null) {
                return Result.error(OrderConstant.ORDER_INFO_ERROR);
            }
            OrderDetailsVO orderDetailsVO = OrderDetailsVO.builder()
                    .userId(order.getUserId())
                    .receiverId(order.getReceiverId())
                    .number(order.getNumber())
                    .begin(order.getBegin())
                    .end(order.getEnd())
                    .earliestTime(order.getEarliestTime())
                    .latestTime(order.getLatestTime())
                    .note(order.getNote())
                    .price(order.getPrice())
                    .payStatus(order.getPayStatus())
                    .orderStatus(order.getOrderStatus())
                    .credit(order.getCredit())
                    .image(thing.getImage())
                    .length(thing.getLength())
                    .width(thing.getWidth())
                    .height(thing.getHeight())
                    .weight(thing.getWeight())
                    .build();
            orderDetailsVOS.add(orderDetailsVO);
        }
        return Result.success(orderDetailsVOS);
    }

    /**
     * 根据地点获取订单
     *
     * @param place 地点
     * @return 订单列表
     */
    @Override
    public List<Order> getOrdersByPlace(String place) {
        List<Order> orders = orderDao.getOrdersByPlace(place);
        return orders;
    }

    /**
     * 更新订单接受者id和订单状态
     *
     * @param receiverId 司机id
     * @param number     订单号
     * @return 是否成功
     */
    @Override
    public Boolean updateReceiverIdAndOrderStatusByNumber(String receiverId, String number) {
        Boolean res = orderDao.updateReceiverIdAndOrderStatusByNumber(receiverId, number) > 0;
        return res;
    }

    /**
     * 添加订单
     *
     * @param order 订单
     * @return 是否成功
     */
    @Override
    public Boolean addOrder(Order order) {
        // 将订单状态设置为未接单
        order.setOrderStatus(OrderConstant.NO_RECEIVED);
        // TODO: 2023/12/20 支付是什么流程
        // 将接单者id设为空（不存在）
        order.setReceiverId(OrderConstant.NO_EXIST);
        // 将评分设置为0
        order.setCredit(OrderConstant.DEFAULT_CREDIT);
        Boolean res = orderDao.addOrder(order);
        return res;
    }

    /**
     * 根据用户id查询用户发布的订单
     *
     * @param userId 用户id
     * @return List<Order>
     */
    @Override
    public Result<List<OrderDetailsVO>> getOrdersByUserId(String userId) {
        List<Order> orders = orderDao.getOrdersByUserId(userId);
        List<OrderDetailsVO> orderDetailsVOS = new ArrayList<>();
        for (Order order : orders) {
            Thing thing = thingDao.getThingByNumber(order.getNumber());
            if (thing == null) {
                return Result.error(OrderConstant.ORDER_INFO_ERROR);
            }
            OrderDetailsVO orderDetailsVO = OrderDetailsVO.builder()
                    .userId(order.getUserId())
                    .receiverId(order.getReceiverId())
                    .number(order.getNumber())
                    .begin(order.getBegin())
                    .end(order.getEnd())
                    .earliestTime(order.getEarliestTime())
                    .latestTime(order.getLatestTime())
                    .note(order.getNote())
                    .price(order.getPrice())
                    .payStatus(order.getPayStatus())
                    .orderStatus(order.getOrderStatus())
                    .credit(order.getCredit())
                    .image(thing.getImage())
                    .length(thing.getLength())
                    .width(thing.getWidth())
                    .height(thing.getHeight())
                    .weight(thing.getWeight())
                    .build();
            orderDetailsVOS.add(orderDetailsVO);
        }
        return Result.success(orderDetailsVOS);
    }

    /**
     * 根据订单编号更新订单
     *
     * @param order 订单
     * @return Integer
     */
    @Override
    public Integer updateOrderByNumber(Order order) {
        Integer res = orderDao.updateByNumber(order);
        return res;
    }

    /**
     * 根据起点或终点查询司机可接的订单
     *
     * @param begin 起点
     * @param end   终点
     * @return List<Order>
     */
    @Override
    public List<OrderDetailsVO> getOrdersByPosition(String begin, String end) {
        return orderDao.getOrdersByPosition(begin, end);
    }

    /**
     * 根据订单编号更新订单的评分
     *
     * @param number 订单编号
     * @param credit 评分
     * @return Integer
     */
    @Override
    public Integer updateCredit(String number, float credit) {
        return orderDao.updateCredit(number, credit);
    }

    /**
     * 根据条件查询订单
     *
     * @param order 订单
     * @return List<Order>
     */
    @Override
    public List<OrderDetailsVO> getOrderByCondition(Order order) {
        return orderDao.getOrdersByCondition(order);
    }
}
